FROM golang:alpine AS builder

#设置gomod代理
ENV GOPROXY https://mirrors.aliyun.com/goproxy/
#启动gomod
ENV GO111MODULE on

#将编译所需必要文件添加过来   // 容器内工作目录
ADD . $GOPATH/src/bot-delivery-server
WORKDIR $GOPATH/src/bot-delivery-server/src

#编译
RUN CGO_ENABLED=1 GOOS=linux go build --ldflags "-extldflags -static" -o bot-delivery-server -a -installsuffix cgo .

#2.构建运行镜像
FROM alpine:latest
# 修改阿里源
RUN sed -i "s/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g" /etc/apk/repositories \
      && apk add --no-cache build-base
RUN apk --no-cache add ca-certificates
#设置时区为上海
RUN apk add tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && echo "Asia/Shanghai" > /etc/timezone
WORKDIR /home/robot_software/bot-delivery-server/src
#将上一阶段的产物复制过来 解决镜像过大的问题
COPY --from=builder /go/src/bot-delivery-server/src/bot-delivery-server .
RUN chmod +x bot-delivery-server

EXPOSE 8000
#启动命令
CMD ["./bot-delivery-server"]

#打包方式：
#源码上传到机器人端，在Dockerfile所在目录bot-delivery-server，执行：
#sudo docker build -t bot-delivery-server:v1.0 .

#机器人端启动方式：
#切换到源码所在目录bot-delivery-server下，执行：
# docker run -itd  --restart=always -p 8000:8000 -v $(pwd)/sql/bot_delivery.db:/home/robot_software/bot-delivery-server/sql/bot_delivery.db \
#  -v $(pwd)/log:/home/robot_software/bot-delivery-server/log \
#  -v $(pwd)/src/config.properties:/home/robot_software/bot-delivery-server/src/config.properties \
#  -v /home/inspur/maps:/home/robot_software/bot-delivery-server/map \
#  -v /home/inspur/robot_program/upload:/home/inspur/robot_program/upload \
#  --name bot-delivery-server_v1.2.0717 bot-delivery-server:v1.2.0717

#启动后，日志文件、数据库文件、配置文件在宿主机，映射到容器内。

#sudo docker run -itd  --restart=always -p 8000:8000 -v $(pwd)/sql/bot_delivery.db:/home/robot_software/bot-delivery-server/sql/bot_delivery.db \
#  -v $(pwd)/log:/home/robot_software/bot-delivery-server/log \
#  -v $(pwd)/src/config.properties:/home/robot_software/bot-delivery-server/src/config.properties \
#  -v /home/inspur/maps:/home/robot_software/bot-delivery-server/map \
#  -v /home/inspur/robot_program/upload:/home/inspur/robot_program/upload \
#  --name bot-delivery-server_v1.0 bot-delivery-server:v1.0

# win docker desktop
#docker run -itd  --restart=always -p 8000:8000 `
# -v ${pwd}/sql/bot_delivery.db:/home/robot_software/bot-delivery-server/sql/bot_delivery.db `
# -v ${pwd}/log:/home/robot_software/bot-delivery-server/log `
# -v ${pwd}/src/config.properties:/home/robot_software/bot-delivery-server/src/config.properties `
# -v ${pwd}/maps:/home/robot_software/bot-delivery-server/map `
# -v ${pwd}/upload:/home/inspur/robot_program/upload `
# --name bot-delivery-server_v1.0 bot-delivery-server:v1.0

# win docker desktop:alpine  略微变小一点
#docker run -itd  --restart=always -p 8000:8000 `
# -v ${pwd}/sql/bot_delivery.db:/home/robot_software/bot-delivery-server/sql/bot_delivery.db `
# -v ${pwd}/log:/home/robot_software/bot-delivery-server/log `
# -v ${pwd}/src/config.properties:/home/robot_software/bot-delivery-server/src/config.properties `
# -v ${pwd}/maps:/home/robot_software/bot-delivery-server/map `
# -v ${pwd}/upload:/home/inspur/robot_program/upload `
# --name bot-delivery-server_alpine bot-delivery-server:alpine
